package cn.seaboot.word.html;

import cn.seaboot.html.HtmlReader;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.IOException;
import java.io.Writer;

/**
 * 读取一个段落的信息
 * <p>
 * 段落一般包含：文字、图片
 *
 * @author Mr.css
 * @version 2025-06-26 14:59
 */
public class ParagraphReader implements HtmlReader<XWPFParagraph> {

    /**
     * 写出数据
     */
    private final Writer writer;

    /**
     * 读取文本域中的文本信息
     */
    private HtmlReader<XWPFRun> reader;


    public ParagraphReader(Writer writer) {
        this.writer = writer;
    }

    public HtmlReader<XWPFRun> getReader() {
        return reader;
    }

    public void setReader(HtmlReader<XWPFRun> reader) {
        this.reader = reader;
    }

    @Override
    public void read(XWPFParagraph para) throws IOException {
        String style = para.getStyle();
        if (style == null) {
            writer.write("<p>");
            for (XWPFRun run : para.getRuns()) {
                reader.read(run);
            }
            writer.write("</p>\r\n");
        } else {
            writer.write("<h" + style + ">" + para.getText() + "</h" + style + ">\r\n");
        }
    }
}
